1. Variables
2. Operations
Inclined to build a profession as core java training? Then here is the blog post on, explore core java training
3. Classes + Objects
- Fields
- Constructors
- Methods
4. Interfaces
VariablesComputer programs, regardless of programming language, typically read data from somewhere (file, keyboard, mouse, network, etc.), process the data, and write some data somewhere again (to screen, file, network, etc.).
In Java, program data is kept in variables. Your Java program first declares the variables, then read data into them, execute operations on the variables, and then write the variables (or data based on the variables) somewhere again. Variables are explained in more detail in the text on Java variables.
Each variable has a data type. The data type determines what kind of data the variable can contain, and what operations you can execute on it. For instance, a variable could be a number. Numbers can be added, subtracted, multiplied, divided, etc. Or, a variable could be a string (text). String's can be divided into substrings, searched for characters, concatenated with other strings, etc. Java comes with a set of built-in data types. These data types are described in more detail in the text on Java data types.
Here is a simple Java variable declaration and operation example. Don't worry if you don't understand it now. Later texts in this Java language tutorial explains the details. The purpose of the example is just to give you a feeling for how working with Java variables look.
int myNumber; myNumber = 0; myNumber = myNumber + 5;
The first line of this example declares a variable named myNumber
of the data type int
. An int
is a 32-bit integer (number without fractions).
The second line sets the value of the myNumber
variable to 0.
The third line adds 5 to the current value of myNumber
Operations in Java are the instructions you can use to process the data in variables. Some operations read and write the values of variables (as you have already seen examples of), while other operations control the program flow. The most important operations are:
1. Variable operations
- Variable assignment of values.
- Variable reading of values.
- Variable arithmetic.
- Object instantiation.
2. Program flow
for
loops.while
loops.if
statements (branches).switch
statements.
3. Method calls.
All of these operations are explained in detail in their own texts.
Here are a few examples of operations:
int number = 0; int abs = 0; //imagine some operations that assign a value to number // - but left out of this example. if(number >= 0) { abs = number; } else { abs = -number; }
This example first declares two variables named number
and abs
. The variable abs
is supposed to contain the absolute value of number
. The absolute value of a number is always positive. For positive numbers, the absolute value is the number itself. For negative numbers, the absolute value is the number without the negative sign. For instance, the absolute value of -10 is 10.
The if
operation checks the value of the number
variable to see if it is larger than or equal to 0. If it is, the absolute value assigned to the abs
variable is the value of the number
variable. If the number
value is less than 0, then the value assigned to number
is equal to -number
. Negating a negative number gives a positive number, remember? -(-10) is 10.
Classes group variables and operations together in coherent modules. A class can have fields, constructors, and methods (plus more, but that is not important now). I will shortly describe fields, constructors, and methods here, but they are explained in more detail in their own texts too.
Objects are instances of classes. When you create an object, that object is of a certain class. The class is like a template (or blueprint) telling how objects of that class should look. When you create an object, you say "give me an object of this class".
If you think of a factory producing lots and lots of the same items, then the class would be the blueprint/manual of how the finished product should look, and the objects would be each of the finished products. If the factory produced cars, then the blueprint/design manual of the cars to produce corresponds to a Java class, and the physical cars produced corresponds to Java objects.
Here is a simple diagram illustrating the principle of objects being of a certain class. The class determines what fields and methods the objects of that class have.
A Java class can contain fields, constructors, and methods
public class Car {Here is an example Java class declaration: }
This example declares a class named Car
. The Car class does not contain any fields, constructors, or methods. It is empty. The example primarily serves to show you an example of how a class declaration looks in Java code.
Fields
A field is a variable that belongs to a class or an object. It is a piece of data, in other words. For instance, a Car class could define the field brand
which all Car objects would have. Each Car object could then have a different value for the brand field.
Fields are covered in more detail in the text on Java fields.
Here is the Car class declaration from above with a field name brand
added:
public class Car { private String brand; }
This example declares a field named brand
of data type String
which is text.
Constructors
Constructors are a special kind of method that is executed when an object of that class is created. Constructors typically initialize the object's internal fields - if necessary.
Constructors are covered in more detail in the text on Java constructors.
Here is the Car class from before with a constructor that initializes the brand
field:
public class Car { private String brand; public Car(String theBrand) { this.brand = theBrand; } }
Methods
Methods are groups of operations that carry out a certain function together. For instance, a method may add to numbers, and divide it by a third number. Or, a method could read and write data in a database, etc.
Methods are typically used when you need to group operations together, that you need to be able to execute from several different places. Or, if you just want your code to be easier to read. In other programming languages, methods may be called "procedures" or "functions".
Methods are covered in more detail in the text on Java methods.
Here is the Car class from before with a single, simple method named getBrand
added:
public class Car { private String brand; public Car(String theBrand) { this.brand = theBrand; } public String getBrand() { return this.brand; } }Interfaces
The interface is a central concept in Java. An interface describes what methods a certain object should have available on it. A class can implement an interface. When a class implements an interface, the class has to implement all the methods described in the interface. Interfaces are described more in my text about Java interfaces.
PackagesPackages in Java is another central concept. A package is a directory containing Java classes and interfaces. Packages provide a handy way of grouping related classes and interfaces, thus making modularization of your Java code easier. Packages are described in more detail in my text about Java pack
Data Types in Java
Java language has a rich implementation of data types. Data types specify size and the type of values that can be stored in an identifier.
In java, data types are classified into two categories :
- Primitive Data type
- Non-Primitive Data type
Primitive Data type
A primitive data type can be of eight types:
- char
- boolean
- byte
- short
- int
- long
- float
- double
Once a primitive data type has been declared its type can never change, although in most cases its value can change. These eight primitive types can be put into four groups.
Integer: This group includes byte
, short
, int
, long
byte: It is an 8-bit integer data type. Value range from -128 to 127. Default value zero. example: byte b=10;
short: It is a 16-bit integer data type. Value range from -32768 to 32767. Default value zero. example:short s=11;
int: It is a 32-bit integer data type. Value range from -2147483648 to 2147483647. Default value zero. example:int i=10;
long: It is a 64-bit integer data type. Value range from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Default value zero. example: long l=100012;
Floating-Point Number
This group includes float, double
float: It is a 32-bit float data type. Default value 0.0f. example: float ff=10.3f
double: It is a 64-bit float data type. Default value 0.0d. example: double db=11.123
Characters: This group represents char, which represent symbols in a character set, like letters and numbers.
char: It is a 16-bit unsigned Unicode character. Range 0 to 65,535. example: char c='a';
Boolean: This group represents boolean, which is a special type for representing true/false values. They have defined as constants of the language. example: boolean b=true;
Non-Primitive(Reference) Data typeA reference data type is used to refer to an object. A reference variable is declared to be specific and that type can never change. We will talk a lot more about reference data type later in the Classes and Object lessons.